AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Initialize()
	self.BaseClass.Initialize(self)
	
	self.Dish = ents.Create("prop_dynamic_override")
	self.Dish:SetModel("models/props_rooftop/roof_dish001.mdl")
	self.Dish:SetPos(self:GetPos()+self:GetAngles():Up()*25)
	self.Dish:SetParent(self)
	self.Dish:SetSolid(SOLID_NONE)
	self.Dish:SetMoveType(MOVETYPE_NONE)
	self.dt.Owner.Tower = self
	self.BombNotify = false
end

function ENT:Use(activator,caller)
	self:CreateTimers()
	self.Active = true
	self:SetColor(255, 255, 255, 255)
	self.Dish:SetColor(255, 255, 255, 255)
end

function scanPlayer(ply, args)
	if args=="" then return "" end
	if(!ply:CanAfford(100)) then
		Notify(ply, 4, 3, "Cannot afford this")
		return ""
	end
	if !ValidEntity(ply.Tower) then
		Notify(ply,4,3,"You must have a Radar Tower to use this command. say /buyradar")
	else
		local userExists = false
		local tower = ply.Tower
		if tower:GetTable().Scans<=0 || !tower:IsPowered() then
			Notify(ply,4,3,"Your radar tower is not charged enough to scan or does not have any power.")
		else
			local target = FindPlayer(args)
			if ValidEntity(target) then
				userExists = true
				umsg.Start("RadarScan")
					umsg.Entity(target)
					umsg.Vector(target:GetPos())
					umsg.Short(target:EntIndex())
				umsg.End()
				local effectdata = EffectData()
					effectdata:SetOrigin(target:GetPos())
					effectdata:SetRadius(512)
				util.Effect("scanring", effectdata)
				tower.Scans = tower.Scans - 1
				ply:TakeMoney(100)
				Notify(ply, 2, 3, "Charged $100 for scan.")
				Notify(ply, 0, 3, "Scanning...")
				Notify(target,1,3, ply:GetName() .. " has scanned you")
				
				if tower.Level>=1 then
					SpyScan(ply,target,false)
				end
				if tower.Level>=2 then
					ReconScan(ply,target)
				end
			end 
			
			if(!userExists) then
				Notify(ply, 4, 3, "Player not found.")
			end
		end
	end
	return ""
end
AddChatCommand("/scan", scanPlayer)

function SpyScan(ply,target,backscan)
	if target:GetNWBool("Scannerd") then
		Notify(ply,4,3,"Could not scan target information due to target having a scanner")
		if !backscan && target!=ply then
			Notify(target, 2, 3, "Using your scanner to scan them back.")
			SpyScan(target,ply,true)
		end
	else
		Notify(ply,2,3,"Printing information on scan target in your console")
		local weapon = "Nothing"
		if ValidEntity(target:GetActiveWeapon()) then
			weapon = target:GetActiveWeapon():GetClass()
		end
		ply:PrintMessage(2, "\n" ..target:GetName() .. "\n" .. target:Health() .. "/" .. target:GetMaxHealth() .. " Health and " .. target:Armor() .. "/100 Armor " ..  "\nHolding weapon: " .. weapon .. "\nOther weapons: \n")
		for k,v in pairs(target:GetWeapons()) do
			if v!=target:GetActiveWeapon() then
				ply:PrintMessage(2, v:GetClass() .. "\n")
			end
		end
		local shield = ""
		local scanner = ""
		local helmet = ""
		if target:GetNWBool("Snipe Shielded") then shield = "shield " end
		if target:GetNWBool("Helmetd") then helmet = "helmet " end
		if target:GetNWBool("Scannerd") then scanner = "scanner " end
		if scanner=="" && helmet == "" && shield == "" then scanner = "Nothing" end
		ply:PrintMessage(2, "Equipped: " .. shield .. helmet .. scanner .. "\n\n")
		// backscan to keep an infinite scan loop from happening back and forth
	end
end

function ReconScan(ply, target)
	Notify(ply,2,3,"Printing a list of things found in scan to your console")
	local scanpos = target:GetPos()
	local stuff = 0
	ply:PrintMessage(2,"\n")
	for k, v in pairs(ents.FindInSphere(scanpos, 512)) do
		if ValidEntity(v) then
			if v:GetTable().Structure then
				stuff = stuff+1
				ply:PrintMessage(2, v.PrintName .. " " .. v.self.dt.Owner:GetName())
			end
		end
	end
	ply:PrintMessage(2,"\n")
	if stuff>0 then
		Notify(ply,2,3,"Scan has found " .. tostring(stuff) .. " structures near " .. target:GetName().."")
	else
		Notify(ply,4,3,"Scan has found nothing")
	end
end

function ENT:Recharge()
	local ply = self.dt.Owner
	if !self:IsPowered() then Notify(ply, 4, 3, "A radar tower has low power, power it to continue charging") return end
	if self.Active then
		self.Scans = self.Scans + 1
		if self.Scans > 10 then
			Notify(ply, 4, 3, "Radar tower is fully charged at 10 charges.")
			self.Scans = 10
		else
			if self.Scans!=1 then
				Notify(ply, 2, 3, "Radar tower is ready to scan. " .. self.Scans .. " Charges available.")
			else
				Notify(ply, 2, 3, "Radar tower is ready to scan. 1 Charge available.")
			end
		end
	else
		Notify(ply, 4, 3, "A radar tower is inactive, press use on it for it to continue charging")
	end
end

function ENT:Detection()
	if self:IsPowered() == false then return end
	local pos = self:GetPos()
	local sawbomb=false
	for k,v in pairs(ents.FindByClass("item_bigbomb")) do
		if v:GetPos():Distance(pos)<2048 then
			sawbomb=true
			if !self.BombNotify then
				Notify(self.dt.Owner,1,3, "Radar tower has detected a Big Bomb")
				self.BombNotify=true
			end
		end
	end
	if !sawbomb then self.BombNotify=false end
end

function ENT:CreateTimers()
	timer.Create(tostring(self) .. "recharge", self.RechargeTime, 0, self.Recharge, self)
	timer.Create(tostring(self) .. "detection", 1, 0, self.Detection, self)
	timer.Create(tostring(self) .. "warnturnoff", self.TurnOffTime - (self.TurnOffTime * 0.1), 1, self.WarnTurnOff, self)
	timer.Create(tostring(self) .. "turnoff", self.TurnOffTime, 1, self.TurnOff, self)
end

function ENT:OnRemove()
	timer.Remove(tostring(self) .. "recharge")
	timer.Remove(tostring(self) .. "warnturnoff")
	timer.Remove(tostring(self) .. "turnoff")
	timer.Remove(tostring(self) .. "detection")
end

